//
//  HJSimplePopWindow.swift
//  APPTestSwift
//
//  Created by hiking on 2020/2/16.
//  Copyright © 2020 hiking. All rights reserved.
//

import UIKit

let CellID = "HJSimplePopWindowCell"
@objc protocol PopWindowDidSelectedDelegate {
    func selectedPopWindowItem(index:Int)
}
class HJSimplePopWindow: UIView,UIGestureRecognizerDelegate {

    weak var delegate:PopWindowDidSelectedDelegate?
    
    lazy var data:NSMutableArray = {
        let data = NSMutableArray.init()
        return data
    }()
    
    //1.声明变量
    lazy var tableView:UITableView = {
        let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.plain)
        tableView.layer.borderColor = UIColor.gray.cgColor
        tableView.layer.borderWidth = 0.5
        tableView.register(HJSimplePopWindowCell.classForCoder(), forCellReuseIdentifier: CellID)
        tableView.separatorStyle = UITableViewCell.SeparatorStyle.none  //设置无分割线
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()
    
    func show() {
        let heightOrig = self.tableView.frame.size.height
        self.tableView.frame.size.height = 0
        self.isHidden = false
        UIView.animate(withDuration: 0.3, animations: {
            self.tableView.frame.size.height = heightOrig
        })
    }
    func close() {
        let heightOrig = self.tableView.frame.size.height
        UIView.animate(withDuration: 0.3, animations: {
            self.tableView.frame.size.height = 0
        }) { (_) in
            self.tableView.frame.size.height = heightOrig
            self.isHidden = true
        }
    }
   
    @objc func tapView() {
        close()
    }
    
    private override init(frame: CGRect) {
           
           super.init(frame:frame)

       }
 
    convenience init(startPoint:CGPoint,data:NSMutableArray) {
        self.init(frame:UIScreen.main.bounds)

        let gestureRecognizer = UITapGestureRecognizer.init(target: self, action:#selector(tapView))
        gestureRecognizer.delegate = self
        self.addGestureRecognizer(gestureRecognizer)
        self.data = data
        //2.初始化视图
        backgroundColor = .clear
        addSubview(self.tableView)
        self.tableView.frame = CGRect.init(x: startPoint.x, y: startPoint.y, width: 100, height: CGFloat(data.count * 30))

        close()
    }
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        if NSStringFromClass(touch.view!.classForCoder) == "UITableViewCellContentView" {
            return false
        }
        return true
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
extension HJSimplePopWindow:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 30
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.delegate?.selectedPopWindowItem(index: indexPath.row)
        close()
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // 如果你注册过Cell，在没有可用的cell时，前者会返回nil；而后者永远都会从注册的nib或者class中替你创建一个可用的Cell。也就是说，前者调用你需要手动检查nil，而后者不需要；
        // 如果你从没有注册过cell，在没有可用的cell时，前者会返回nil，后者……直接崩溃！也就是说，调用后者你 必须确保注册过cell 。
        let cell = tableView.dequeueReusableCell(withIdentifier: CellID,for: indexPath)
        cell.textLabel?.text = data[indexPath.row] as? String
        return cell
    }
}
